package com.jcfk.system.controller;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import com.jcfk.system.domain.dto.SysPhoneCallDTO;
import com.jcfk.system.domain.query.SysPhoneCallQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import com.jcfk.system.service.SysPhoneCallService;

/**
 * 呼叫记录
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-09-20
 */
@Api(tags = {"呼叫记录管理"})
@RestController
@RequestMapping("sysphonecall")
public class SysPhoneCallController {
    @Autowired
    private SysPhoneCallService sysPhoneCallService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<SysPhoneCallQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询呼叫记录")
    @PostMapping("/queryByPage")
    public RetPage<SysPhoneCallDTO> queryByPage(@RequestBody ReqPageParam<SysPhoneCallQuery> requestParam) {
        DataPage<SysPhoneCallDTO> page = new DataPage<SysPhoneCallDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                SysPhoneCallDTO.class);
        page = sysPhoneCallService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 呼叫记录
     * 根据id 查询详情
     */
    @ApiOperation("查询呼叫记录")
    @GetMapping("/get/{id}")
    public RetResult<SysPhoneCallDTO> get(@PathVariable("id") String id) {
        SysPhoneCallDTO item = sysPhoneCallService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理呼叫记录
     */
    @ApiOperation("保存呼叫记录")
    @PostMapping("/save")
    @RequestLogHeader(modular = "呼叫记录管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid SysPhoneCallDTO sysPhoneCall) {
        // 数据校验
        RetResult ret = sysPhoneCallService.isValid(sysPhoneCall);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(sysPhoneCall.getId())) {
            // 新增
            sysPhoneCallService.insert(sysPhoneCall);
        } else {
            // 更新
            sysPhoneCallService.update(sysPhoneCall);
        }
        return RetResult.ok();

    }

    /**
     * 批量删除呼叫记录
     */
    @ApiOperation("批量删除呼叫记录")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "呼叫记录管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody SysPhoneCallDTO sysPhoneCall) {
        return RetResult.ok(sysPhoneCallService.removeBigBatch(sysPhoneCall.getIds()));
    }


    /**
     * 删除呼叫记录
     */
    @ApiOperation("删除呼叫记录")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "呼叫记录管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        sysPhoneCallService.deleteItem(id);
        return RetResult.ok();
    }
}
